package vn.gq.udv.comic.model.item;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class Bookmark implements Parcelable {
	
	private String chapterId = "";
	private String title = "";
	private int position = 0;
	
	public Bookmark() {
		
	}

	public Bookmark(ChapterItem item, int position) {
		this.position = position;
		title = item.getTitle();
		chapterId = item.getChapterId();
	}
	
	public Bookmark(Parcel in) {
		chapterId = in.readString();
		title = in.readString();
		position = in.readInt();
	}
	
	public Bookmark(String json) {
		try {
			JSONObject in = new JSONObject(json);
			chapterId = in.getString("chapterId");
			title = in.getString("title");
			position = in.getInt("position");
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		dest.writeString(chapterId);
		dest.writeString(title);
		dest.writeInt(position);
	}
	
	public JSONObject toJSONObject() {
		JSONObject out = new JSONObject();
		try {
			out.put("chapterId", chapterId);
			out.put("title", title);
			out.put("position", position);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return out;
	}
	
	public static final Parcelable.Creator<Bookmark> CREATOR = new Creator<Bookmark>() {
		
		@Override
		public Bookmark[] newArray(int size) {
			return new Bookmark[size];
		}
		
		@Override
		public Bookmark createFromParcel(Parcel source) {
			return new Bookmark(source);
		}
	};
	
	public String getChapterId() {
		return chapterId;
	}

	public void setChapterId(String chapterId) {
		this.chapterId = chapterId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getPosition() {
		return position;
	}

	public void setPosition(int position) {
		this.position = position;
	}
}
